import areaPicker from './area-picker'

export default{
	install:function(Vue,options){
		const AREAPICKER_BUS=new Vue({
			data(){
				return{
					isShow:false
				}
			},
			methods:{
				show:function(){
					this.isShow=true;
				},
				hide:function(){
					this.isShow=false;
				}
			}
		})
		window.AREAPICKER_BUS = AREAPICKER_BUS;
		Vue.component("area-picker",areaPicker);
		Vue.directive("areapicker",{
			bind:function(el,binding,vnode,oldVnode){
				el.onclick=function(ev){
					var oEvent = ev||event;
					oEvent.stopPropagation();
					oEvent.cancelBubble=true;
					var paramArr=binding.expression.split(",");
					if(paramArr.length!=3){console.error("参数不正确");return false;}
					var address="";
					var vmObj=vnode.context;
					for(var index in paramArr){
						if(paramArr[index].trim().length>0&&vmObj[paramArr[index]].trim().length>0){
							address+=vmObj[paramArr[index]]+",";
						}
					}
					if(address.length>0)address=address.substring(0,address.length-1);
					var eventBus=window.AREAPICKER_BUS;
					eventBus.$off("confirm");
					eventBus.show();
					eventBus.$emit("chooseAddress",address);
					eventBus.$on("confirm",(val)=>{
						var arr=val.split(",");
						for(var index in paramArr){
							vmObj[paramArr[index]]=arr[index];
						}
					})
				}
			}
		})
	}
}
